﻿using System;

using System.Collections.Generic;
using System.Text;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Abstract class that receives events and provides default handler.
    /// </summary>
    public abstract class NotifyEventControl : EventControl
    {
        public override void OnMouseClick(MouseClickEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                switch (e.MouseClickType)
                {
                    case MouseClickType.SingleClick: OnClick(); break;
                    case MouseClickType.DoubleClick: OnDblClick(); break;
                }
            }
        }

        protected virtual void OnClick()
        {
            if (Click != null) Click(this, EventArgs.Empty);
        }

        public virtual void OnDblClick()
        {
            if (DblClick != null) DblClick(this, EventArgs.Empty);
        }

        public override void OnMouseDown(MouseDownEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                if (MouseDown != null) MouseDown(this, e);
            }
        }

        public override void OnMouseUp(MouseUpEventArgs e)
        {
            if (!e.Handled)
            {
                if (e.AllowDoubleClick) e.AllowDoubleClick = DblClick == null;
                e.Handled = true;
                if (MouseUp != null) MouseUp(this, e);
            }
        }

        public override void OnMouseMove(MouseMoveEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                if (MouseMove != null) MouseMove(this, e);
            }
        }

        public override void OnGotFocus()
        {
            base.OnGotFocus();
            OnFocusChanged();
        }

        public override void OnLostFocus()
        {
            base.OnLostFocus();
            OnFocusChanged();
        }

        private void OnFocusChanged()
        {
            if (FocusChanged != null) FocusChanged(this, EventArgs.Empty);
        }

        public virtual bool CanFocus { get { return true; } }

        public event EventHandler FocusChanged;
        public event EventHandler Click;
        public event EventHandler DblClick;
        public event EventHandler<MouseMoveEventArgs> MouseMove;
        public event EventHandler<MouseDownEventArgs> MouseDown;
        public event EventHandler<MouseUpEventArgs> MouseUp;
    }
}
